package com.cardbaobao.cardbabyclient.ui.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.buried.Self;
import com.cardbaobao.cardbabyclient.ui.activity.base.BaseActivity;
import com.cardbaobao.cardbabyclient.utils.ApplicationHelper;
import com.cardbaobao.cardbabyclient.utils.BitmapUtil;
import com.cardbaobao.cardbabyclient.utils.ScreenUtil;
import com.cardbaobao.cardbabyclient.utils.UmengEventUtils;
import com.cardbaobao.cardbabyclient.expand.transformer.ScaleAlphaTransformer;

import butterknife.BindView;

/**
 * Created by Administrator on 2017/1/11.
 */
public class FirstShowActivity extends BaseActivity {
    public static final String APP_FIRST_START_B = "active_first_start";

    @BindView(R.id.viewpager)
    ViewPager viewPager;

    private FirstShowViewPagerAdapter vpAdapter;
    /**
     * 页面是否是拖动状态
     */
    private boolean isPageScrollState = false;
    /**
     * 引导图片资源
     */
    private static final int[] pics = {
            R.mipmap.img_guid_1,
            R.mipmap.img_guid_2,
            R.mipmap.img_guid_3
    };


    private Bitmap[] mBitmaps = new Bitmap[pics.length];

    private ImageView[] mImageViews = new ImageView[pics.length];

    @Override
    protected void initView(Bundle savedInstanceState) {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        mCurrentPageId= Self.Page.app_guide_page;

        vpAdapter = new FirstShowViewPagerAdapter();
        viewPager.setPageTransformer(true, new ScaleAlphaTransformer());//设置ViewPager滑动动画
        viewPager.setAdapter(vpAdapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                switch (state) {
                    case ViewPager.SCROLL_STATE_DRAGGING:
                        // 正在拖动页面时执行此处,拖动时才跳转到下一页
                        isPageScrollState = false;
                        break;
                    case ViewPager.SCROLL_STATE_SETTLING:
                        //pager正在自动沉降，相当于松手后，pager恢复到一个完整pager的过程
                        isPageScrollState = true;
                        break;
                    case ViewPager.SCROLL_STATE_IDLE:
                        //pager处于空闲状态
                        if (viewPager.getCurrentItem() == vpAdapter.getCount() - 1 && !isPageScrollState) {
                            //最后一页，并且当前不是拖动状态,执行跳转到首页
                            toMainActivity();
                        }
                        isPageScrollState = true;
                        break;
                }
            }
        });
        mController.setFirstStart();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_first_show;
    }

    private void toMainActivity(){
        Intent indexIntent = new Intent();
        indexIntent.setClass(FirstShowActivity.this, MainActivity.class);
        //告诉MainActivity，APP是第一次启动
        indexIntent.putExtra(APP_FIRST_START_B,true);
        indexIntent.putExtra(PRE_PAGE_ID_S,mCurrentPageId);
        startActivity(indexIntent);
        finish();
    }

    @Override
    protected void onResume() {
        super.onResume();
        UmengEventUtils.pageEvent(mContext,
                ApplicationHelper.getApplicationContext().getString(R.string.event_0x001));
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return true;
        }
        return false;
    }

    private class FirstShowViewPagerAdapter extends PagerAdapter{

        private int mWidth;

        private int mHeight;

        private FirstShowViewPagerAdapter(){
            mWidth = ScreenUtil.getScreenWidth(mContext);
            mHeight = ScreenUtil.getScreenHeight(mContext);
        }

        @Override
        public int getCount() {
            return pics.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {

            if(mImageViews[position] == null){
                mImageViews[position] = new ImageView(mContext);
            }
            if(mBitmaps[position] == null){
                mBitmaps[position] = BitmapUtil.getSmallBitmap(mContext,pics[position],mWidth,mHeight);
            }
            mImageViews[position].setImageBitmap(mBitmaps[position]);
            mImageViews[position].setScaleType(ImageView.ScaleType.FIT_XY);
            //添加View到容器
            container.addView(mImageViews[position]);
            //返回当前的View
            if(position==mImageViews.length-1){
                //最后一项，点击时跳转
                mImageViews[position].setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        toMainActivity();
                    }
                });
            }
            return mImageViews[position];
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        for(int i = 0;i < mBitmaps.length;i++){
            Bitmap bitmap = mBitmaps[i];
            if(bitmap != null && !bitmap.isRecycled()){
                bitmap.recycle();
            }
            mImageViews[i] = null;
        }
    }
}
